//
//  TDUserInfo.m
//  GoodCoach
//
//  Created by yuan wei on 13-10-30.
//  Copyright (c) 2013年 yuan wei. All rights reserved.
//

#import "JHUserInfo.h"

@implementation JHUserInfo

//文件路径
+ (NSString *)filePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"userInfo.plist"];
    return filePath;
}

+ (BOOL)checkFilePathForFile
{
    NSString *filepath = [JHUserInfo filePath];
    NSFileManager *fileManger = [NSFileManager defaultManager];
    if ( [fileManger fileExistsAtPath:filepath] )
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

//读取文件
+ (NSMutableDictionary *)getFile
{
    NSString *filepath = [JHUserInfo filePath];
    return [NSMutableDictionary dictionaryWithContentsOfFile:filepath];
}

//把用户信息写入文件
+ (void)save:(NSDictionary *)dictionary
{
    NSString *filepath = [JHUserInfo filePath];
    [dictionary writeToFile:filepath atomically:NO];
}

//保存用户信息
+ (void)saveUserInfo:(NSDictionary *)userInfo
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [userInfo enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
     {
         if (![obj isEqual:[NSNull null]])
         {
             [dic setObject:obj forKey:key];
         }
     }];
    [JHUserInfo save:dic];
}

//获取用户信息
+ (UserInfo *)getUserInfo
{
    if ([self checkFilePathForFile])
    {
        NSMutableDictionary *dic = (NSMutableDictionary*)[NSDictionary dictionaryWithContentsOfFile:[JHUserInfo filePath]];
        return [UserInfo mj_objectWithKeyValues:dic];
    }
    else
    {
        return nil;
    }
}

+ (void)removeUserInfo
{
    if ([self checkFilePathForFile])
    {
        NSFileManager* fm = [NSFileManager defaultManager];
        [fm removeItemAtPath:[JHUserInfo filePath] error:nil];
        //[[TDUserInfo getChildInfo] removeAllObjects];
        
    }
}

@end
